package hygprs

import (
	"encoding/binary"
)

// 包序号值
var PacketSeq int32

type Packet struct {
	Header       byte
	Version      byte
	Type         byte
	SerialNumber uint16
	Data         []byte // 包数据
}

func MakePacket(data []byte) Packet {
	return Packet{
		Header:       data[PositionHeader],
		Version:      data[PositionVersion],
		Type:         data[PositionType],
		SerialNumber: ToUint16(data[PositionSerialNumber:PositionType]),
		Data:         data,
	}
}

func (p Packet) Pack() []byte {
	offset := len(p.Data) - 5
	binary.BigEndian.PutUint16(p.Data[1:], uint16(len(p.Data)))
	binary.BigEndian.PutUint16(p.Data[4:], p.SerialNumber)
	binary.BigEndian.PutUint32(p.Data[offset:], CheckSum(p.Data[:offset]))
	return p.Data
}
